﻿
using SqlSugar;
using WCS.Entities.Enum;

namespace WCS.Entities
{
    public class Job
    {

        public Job()
        {

        }

        public Job(TransferPort port)
        {
            Id = DateTime.Now.ToString("yyMMddHHmmssfff");
            CreateTime = DateTime.Now;
            Status = TaskState.Pending;
            Priority = port.Priority;
            Type = port.CallType;
            Region = port.Region;
            Target = port.Id;
            Content = port.Content;
        }

        public Job(TransferPort port, int seconds)
        {
            Id = DateTime.Now.AddSeconds(seconds).ToString("yyMMddHHmmssfff");
            CreateTime = DateTime.Now;
            Status = TaskState.Pending;
            Priority = port.Priority;
            Type = port.CallType;
            Region = port.Region;
            Target = port.Id;
            Content = port.Content;
        }

        #region Properties

        /// <summary>
        /// 任务Id
        /// </summary>
        [SugarColumn(IsPrimaryKey = true)]
        public string Id { get; set; }


        #region 时间

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }


        /// <summary>
        /// 开始执行时间
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public DateTime? BeginTime { get; set; }


        /// <summary>
        /// 任务更新时间
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public DateTime? UpdateTime { get; set; }

        #endregion

        /// <summary>
        /// 任务状态
        /// 分为未执行，执行中，已完成，被取消，被打断
        /// </summary>
        public TaskState Status { get; set; }

        /// <summary>
        /// 任务优先级,数值越大优先级越高
        /// </summary>
        public int Priority { get; set; }

        /// <summary>
        /// 任务类型 
        /// 1.接料任务,
        /// 2.送料任务,
        /// 3.充电任务
        /// 4.停车任务
        /// </summary>
        public TaskType Type { get; set; }

        public OperationType Action { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string Remark { get; set; }

        /// <summary>
        /// 任务物料号
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string Content { get; set; }

        /// <summary>
        /// 所属区域
        /// </summary>
        public string Region { get; set; }

        /// <summary>
        /// 目标点
        /// </summary>
        public string Target { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool Isdeleted { get; set; }

        public void UpdateStatus(TaskState state, string remark = "")
        {
            UpdateTime = DateTime.Now;
            Status = state;
            Remark = remark;
        }

        #endregion




        #region Override

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (ReferenceEquals(obj, this))
            {
                return true;
            }
            if (obj.GetType() != GetType())
            {
                return false;
            }
            return obj is Job b && b.Id == Id;

        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public override string ToString()
        {

            return $"Job Info Is:Id({Id}),TaskType:{Type},TaskState:{Status}, BeginTime({BeginTime?.ToString("MM-dd HH:mm:ss") ?? ""}),UpdateTime({UpdateTime?.ToString("MM-dd HH: mm: ss") ?? ""}),Target:{Target}";
        }

        #endregion
    }
}
